package com.learn.hibernatevalidator.chapter05.groupredefined;

import jakarta.validation.ConstraintViolation;
import jakarta.validation.GroupSequence;
import jakarta.validation.Validation;
import jakarta.validation.Validator;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.groups.Default;
import lombok.Data;

import java.util.Set;

interface Insert {
}


interface Update {
}

/**
 * 重新定义默认组
 *
 * @author: liyf
 * @create: 2025-01-06 22:19
 **/
public class Example {


    public static void main(String[] args) {

        User user = new User();
        user.setId("32");
        user.setId2("32");

        Validator validator = Validation.buildDefaultValidatorFactory().getValidator();

        Set<ConstraintViolation<User>> validate = validator.validate(user, Default.class);

        System.out.println(validate.size());

        for (ConstraintViolation<User> userConstraintViolation : validate) {
            System.out.println(userConstraintViolation.getPropertyPath() + " " + userConstraintViolation.getMessage());
        }

    }
}

/**
 * 表示 在当前类中将 Insert.class 设置为 默认组， 进行校验时要验证的组设置为 Default 和 Insert 效果是一样的
 */
@GroupSequence({
        Insert.class,
        Update.class,
        User.class
})
@Data
class User {

    @NotNull(groups = Insert.class)
    private String id;

    @NotNull(groups = Insert.class)
    private String id2;

    @NotNull
    private String name;

    @NotNull
    private String gender;

    @NotNull(groups = Update.class)
    private String address;
}